package com.spring.security.service;

import com.spring.security.entity.SysUser;
import org.apache.log4j.Logger;
import org.springframework.security.access.PermissionEvaluator;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;

import java.io.Serializable;
import java.util.Objects;

/**
 * ClassName: CumPermissionEvaluator
 * Desc:
 * Date： 2017/5/27
 * Created：shaom
 */
public class CumPermissionEvaluator implements PermissionEvaluator {
    private final Logger logger = Logger.getLogger(CumPermissionEvaluator.class);

    @Override
    public boolean hasPermission(Authentication authentication, Object targetDomainObject, Object permission) {

        if (isNull(authentication) || !(permission instanceof String)) {
            return false;
        }

//        String targetType = targetDomainObject.getClass().getSimpleName().toUpperCase();
        SysUser principal = (SysUser) authentication.getPrincipal();
        logger.debug(principal);

        for (GrantedAuthority grantedAuthority : authentication.getAuthorities()) {
            logger.debug(grantedAuthority.getAuthority());

        }
        return true;
    }

    @Override
    public boolean hasPermission(Authentication authentication, Serializable targetId, String targetType, Object permission) {
        return false;
    }


    private boolean isNull(Object... obj) {
        for (Object o : obj) {
            if (Objects.isNull(o)) {
                return true;
            }
        }
        return false;
    }
}
